import logging
from common.config import singleton



@singleton
class Log:
    def __init__(self, log_file='app.log'):
        self.logger = logging.getLogger(name=__name__)
        self.logger.setLevel(logging.DEBUG)

        # 创建FileHandler，用于写入日志文件
        fh = logging.FileHandler(log_file)
        fh.setLevel(logging.DEBUG)

        # 创建Formatter，设定日志格式
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)

        # 将FileHandler添加到Logger中
        self.logger.addHandler(fh)

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def warning(self, message):
        self.logger.warning(message)

    def error(self, message):
        self.logger.error(message)

    def critical(self, message):
        self.logger.critical(message)